﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using cbmobile.DAC;
using PQT.API;
using System.Data;
using cbmobile.BLC;
using System.Data.SqlClient;

namespace cbmobile.BLC
{
    public class BLC_PhuKien
    {
        private CBMobileEntities da = null;
        SqlConnection cnn = null;
        DBConnection dbCn = new DBConnection("PQT");
        public BLC_PhuKien()
        {
            da = new CBMobileEntities();
        }

        public IList<PhuKien> All_PhuKien()
        {
            return da.PhuKiens.ToList();
        }


        public PhuKien Get_PhuKien_By_ID(int Ma_PhuKien)
        {
            IList<PhuKien> list = da.PhuKiens.Where(z => z.Ma_PhuKien == Ma_PhuKien).ToList();
            if (list.Count() == 0)
                return null;
            return list.First();
        }

        public IList<PhuKien> Row_PhuKien(int p_page, int p_pageSize, string p_name)
        {
            int preCount = (p_page - 1) * p_pageSize;
            IList<PhuKien> list = da.PhuKiens.Where(z => z.Ten_PhuKien.Contains(p_name)).OrderByDescending(z => z.Ten_PhuKien).Skip(preCount).Take(preCount + p_pageSize).ToList();
            return list;
        }

        public DataTable Get_PK_Top()
        {
            DataTable dt = null;
            using (cnn = dbCn.InitConnection())
            {
                cnn.Open();
                SqlDataAdapter da = null;
                DataSet ds = new DataSet();
                SqlCommand cmdUp = new SqlCommand("pr_Get_PK_Top", cnn);
                cmdUp.CommandType = CommandType.StoredProcedure;
                try
                {
                    da = new SqlDataAdapter(cmdUp);
                    dt = new DataTable();
                    da.Fill(dt);
                }
                catch { }
                finally
                {
                    if (cnn.State == ConnectionState.Open)
                        cnn.Close();
                    cnn.Dispose();
                }

            }
            return dt;
        }


        public LoaiPhuKien Get_PK_By_NamePK(string TenLoai_PK)
        {
            IList<LoaiPhuKien> list = da.LoaiPhuKiens.Where(z => z.TenLoaiPhuKien.Contains(TenLoai_PK)).ToList();
            if (list.Count() == 0)
                return null;
            return list.First();
        }

        public IList<PhuKien> Get_PK_By_IDMaPK(int MaPK)
        {
            IList<PhuKien> list = da.PhuKiens.Where(z => z.Ma_LoaiPhuKien == MaPK).ToList();
            if (list.Count() == 0)
                return null;
            return list;
        }

        public int GetTotalRecord(string p_name)
        {
            return da.PhuKiens.Where(z => z.Ten_PhuKien.Contains(p_name)).Count();
        }

        public void Delete(int Ma_PhuKien)
        {
            var existing = da.PhuKiens.Where(p => p.Ma_PhuKien == Ma_PhuKien).First();
            existing.TinhTrang = (int)CommonStatus.Delete;
            da.SaveChanges();
        }


        #region create
        public void Create(int Ma_LoaiPhuKien, string Ten_PhuKien, int TinhTrang, string ChatLieu,
                                string MauSac, string NguyenHop,
                                string BaoHanh, decimal Gia, int SoLuotXem, string MoTa, string BaiViet,
                                string HinhAnh, DateTime NgayTao)
        {
            var objEnt = new PhuKien();
            objEnt.Ma_LoaiPhuKien = Ma_LoaiPhuKien;
            objEnt.Ten_PhuKien = Ten_PhuKien;
            objEnt.TinhTrang = TinhTrang;
            objEnt.ChatLieu = ChatLieu;
            objEnt.MauSac = MauSac;
            objEnt.NguyenHop = NguyenHop;
            objEnt.BaoHanh = BaoHanh;
            objEnt.Gia = Gia;
            objEnt.SoLuotXem = SoLuotXem;
            objEnt.HinhAnh = HinhAnh;
            objEnt.MoTa = MoTa;
            objEnt.BaiViet = BaiViet;
            objEnt.NgayTao = NgayTao;

            da.PhuKiens.AddObject(objEnt);
            da.SaveChanges();
        }
        #endregion

        #region update
        public void Update(int Ma_PhuKien, int Ma_LoaiPhuKien, string Ten_PhuKien, int TinhTrang, string ChatLieu,
                                string MauSac, string NguyenHop,
                                string BaoHanh, decimal Gia, int SoLuotXem, string MoTa, string BaiViet,
                                string HinhAnh, DateTime NgayTao)
        {
            var role = new PhuKien();
            var objEnt = da.PhuKiens.Where(p => p.Ma_PhuKien == Ma_PhuKien).First();

            objEnt.Ma_LoaiPhuKien = Ma_LoaiPhuKien;
            objEnt.Ten_PhuKien = Ten_PhuKien;
            objEnt.TinhTrang = TinhTrang;
            objEnt.ChatLieu = ChatLieu;
            objEnt.MauSac = MauSac;
            objEnt.NguyenHop = NguyenHop;
            objEnt.BaoHanh = BaoHanh;
            objEnt.Gia = Gia;
            objEnt.SoLuotXem = SoLuotXem;
            objEnt.HinhAnh = HinhAnh;
            objEnt.MoTa = MoTa;
            objEnt.BaiViet = BaiViet;
            objEnt.NgayTao = NgayTao;

            da.SaveChanges();
        }
        #endregion

        public void Create(PhuKien role)
        {
            da.PhuKiens.AddObject(role);
            da.SaveChanges();
        }
    }
}